package com.atguigu.gmall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 *
 */
@Configuration
public class CorsConfig {


    /**
     * 配置全局跨域操作
     * @return
     */
    @Bean
    public CorsWebFilter corsWebFilter() {


        CorsConfiguration corsConfiguration = new CorsConfiguration();
        //设置允许的域
        corsConfiguration.addAllowedOrigin("*");
        //设置允许携带cookie
        corsConfiguration.setAllowCredentials(true);
        //设置允许访问的方式
        corsConfiguration.addAllowedMethod("*");
        //设置允许携带的头信息
        corsConfiguration.addAllowedHeader("*");
        //设置预检的时间
        corsConfiguration.setMaxAge(3600000l);


        //创建跨域配置对象
        UrlBasedCorsConfigurationSource configurationSource = new UrlBasedCorsConfigurationSource();
        //设置
        configurationSource.registerCorsConfiguration("/**", corsConfiguration);

        return new CorsWebFilter(configurationSource);
    }
}
